package thread.src.create;

/**
 * 线程创建方式二:实现接口,不会限制继承,扩展性好
 */
public class ThreadDemo2Other {
    public static void main(String[] args) {
        //直接用匿名内部类给Runnable接口创建一个对象
        Runnable r = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println("子线程1执行输出:" + i);
                }
            }
        };
        //创建任务对象并交给线程对象处理
        Thread t = new Thread(r);
        //启动线程
        t.start();

        //写法可以进一步简化
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println("子线程2执行输出:" + i);
                }
            }
        }).start();

        for (int i = 0; i < 10; i++) {
            System.out.println("主线程执行输出:" + i);
        }
    }
}
